/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DetectarLineas;

import java.awt.Color;
import java.awt.image.BufferedImage;

/**
 *
 * @author Joaquín  Ortiz Díaz
 */
public class Paso2 {

    public BufferedImage aplica_paso2(BufferedImage a){

        BufferedImage ejemplo = new BufferedImage(a.getWidth(), a.getHeight(), a.getType());

        int width=a.getWidth();
        int height= a.getHeight();

        
        //Hay que calcular el W, esto es de prueba
        int w=5;

        for(int i=0;i<width;i++){
            for(int j=0; j<height;j++){

                if((SC(3,i,j,a,w) > SL(3,i,j,a,w))&& (SC(3,i,j,a,w)>SR(3,i,j,a,w))){
                    int rojo=2*SC(1,i,j,a,w) - SL(1,i,j,a,w) - SR(1,i,j,a,w);
                    int verde=2*SC(2,i,j,a,w) - SL(2,i,j,a,w) - SR(2,i,j,a,w);
                    int azul=2*SC(3,i,j,a,w) - SL(3,i,j,a,w) - SR(3,i,j,a,w);

                    if(rojo>255)
                        rojo=255;

                    if(azul>255)
                        azul=255;

                    if(verde>255)
                        verde=255;

                    ejemplo.setRGB(i, j, (new Color(rojo,verde,azul)).getRGB());

                }else
                    ejemplo.setRGB(i, j, (new Color(0,0,0)).getRGB());
                   
            }
        }

        return ejemplo;
    }

    public int SL(int k, int i, int j, BufferedImage a,int w){
        int suma=0;
        int color=0;
        for(int l=(j-(3*w/2)); l<=(j-w/2-1);l++){
            if(l>=0 && l<a.getHeight()){
                int pixel = a.getRGB(i, l);
                if(k==1){
                    color=(pixel & 0x00ff0000) >> 16;
                    suma=suma+color;
                }else if (k==2){
                    color=(pixel & 0x0000ff00) >> 8;
                    suma=suma+color;
                }else{
                    color=(pixel & 0x000000ff) >> 0;
                    suma=suma+color;
                }
            }
        }
        return suma;
    }

    
    public int SC(int k, int i, int j, BufferedImage a,int w){
        int suma=0;
        int color=0;
        for(int l=(j-w/2); l<=(j+w/2);l++){
            if(l>=0 && l<a.getHeight()){
                int pixel = a.getRGB(i, l);
                if(k==1){
                    color=(pixel & 0x00ff0000) >> 16;
                    suma=suma+color;
                }else if (k==2){
                    color=(pixel & 0x0000ff00) >> 8;
                    suma=suma+color;
                }else{
                    color=(pixel & 0x000000ff) >> 0;
                    suma=suma+color;
                }
            }
        }
        return suma;
    }
    public int SR(int k, int i, int j, BufferedImage a,int w){
        int suma=0;
        int color=0;
        for(int l=(j+w/2+1); l<=(j+(3*w/2));l++){
            if(l>=0 && l<a.getHeight()){
                int pixel = a.getRGB(i,l);
                if(k==1){
                    color=(pixel & 0x00ff0000) >> 16;
                    suma=suma+color;
                }else if (k==2){
                    color=(pixel & 0x0000ff00) >> 8;
                    suma=suma+color;
                }else{
                    color=(pixel & 0x000000ff) >> 0;
                    suma=suma+color;
                }
            }
        }
        return suma;
    }

    public int calculaW (BufferedImage a){
        int anc = a.getWidth();
        int alt = a.getHeight();
        int varianzas[][] = null;
        int w = 0;
        int sumk=0;
        int sumi=0;
        int pix;

        for(int j=0; j<anc; j++){
            for(int i=0; i<alt; i++){
                for(int k=0; k<alt; k++){
                 pix = a.getRGB(j,k);
                 sumk = sumk + (1/alt)*((pix & 0x000000ff)>> 0);
                }
                pix = a.getRGB(j,i);
                sumi = sumi + (1/alt)*((((pix & 0x000000ff)>> 0) - sumk)^2);
                varianzas[i][j]=sumi;
            }

        }

        return w; //XD ya esta echo :P
    }
}
